package com.guat.service;

import com.guat.dao.ArticleDao;
import com.guat.dao.UserDao;
import com.guat.entity.Article;
import com.guat.entity.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserService {
    //获取所有用户
    public List<User> getAll(){
        UserDao userDao = new UserDao();
        return userDao.getUserAll();
    }
    //用户登录
    public Map<String, Object> getUser(String username, String password) {
        UserDao userDao = new UserDao();
        User user = userDao.login(username);
        //判断数据库账号密码是否与前端输入相等
        Map<String, Object> map = new HashMap<>();
        if (user.getUsername()!=null){
            if (user.getPassword().equals(password)){
                if (user.getType().equals("管理员")){
                    map.put("code",201);
                    map.put("msg","登录成功");
                }else{
                    map.put("code",202);
                    map.put("msg","登录成功");
                }
            } else {
                map.put("code",304);
                map.put("msg","登录失败！密码错误");
            }
        }else {
            map.put("code",400);
            map.put("msg","登录失败！用户名不存在");
        }
        return map;
    }
    //用户注册
    public Map<String, Object> addUser(String username,String password,String type) {
        UserDao userDao = new UserDao();
        Map<String, Object> map = new HashMap<>();
        //先调用登录方法,查询用户名是否已经存在,如果已经存在就不能注册
        User loginUser = userDao.login(username);
        if (username!=""&&password!=""){
            if (loginUser.getUsername()==null){
                userDao.addUser(username, password,type);
                map.put("code",200);
                map.put("msg","注册成功,快去登录吧！");
            } else {
                map.put("code",400);
                map.put("msg","注册失败,账号已存在！");
            }
        } else {
            map.put("code",304);
            map.put("msg","注册失败,账号和密码不能为空！");
        }
        return map;
    }
    //删除用户
    public Map<String, Object> deleteUser(String id){
        UserDao userDao = new UserDao();
        Map<String, Object> map = new HashMap<>();
        boolean flag = userDao.deleteUser(id);
        if (flag) {
            map.put("code",200);
            map.put("msg","删除成功！");
        } else {
            map.put("code",400);
            map.put("msg","删除失败,用户不存在！");
        }
        return map;
    }
    //先根据id获取想要修改用户的信息,响应给前端
    public Map<String, Object> doUpdateUser(String id){
        UserDao userDao = new UserDao();
        Map<String, Object> map = new HashMap<>();
        User user = userDao.doUpdateUser(id);
        map.put("code",200);
        map.put("msg","查询成功！");
        map.put("data",user);
        return map;
    }

    //根据id修改用户的信息
    public Map<String, Object> updateUser(String id,String username,String password,String type){
        UserDao userDao = new UserDao();
        Map<String, Object> map = new HashMap<>();
        userDao.updateUser(id, username, password, type);
        map.put("code",200);
        map.put("msg","修改成功！");
        return map;
    }

    //根据搜索值获取文章
    public List<User> searchUser(String value){
        UserDao userDao = new UserDao();
        return userDao.searchUser(value);
    }

}
